package org.mq.mymq.broker.core;

import org.mq.mymq.broker.cache.CommonCache;
import org.mq.mymq.broker.model.MyMqTopicModel;
import org.mq.mymq.broker.model.QueueModel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ConsumeQueueAppendHandler
 * @Description Cre
 * @Author jiarong_ye
 * @Date 2025/6/16 20:44
 * @Version 1.0
 */
public class ConsumeQueueAppendHandler {
    public void prepareConsumeQueue(String topicName) throws IOException {
        List<ConsumeQueueMMapFileModel> consumeQueueMMapFileModels = new ArrayList<>();
        MyMqTopicModel myMqTopicModel = CommonCache.getMyMqTopicModelsMap().get(topicName);
        List<QueueModel> queueList = myMqTopicModel.getQueueList();
        for (QueueModel queueModel : queueList) {
            ConsumeQueueMMapFileModel consumeQueueMMapFileModel = new ConsumeQueueMMapFileModel();
            consumeQueueMMapFileModel.loadFileInMMap(
                    topicName,
                    queueModel.getId(),
                    queueModel.getLastOffset(),
                    queueModel.getLatestOffset().get(),
                    queueModel.getOffsetLimit());
            consumeQueueMMapFileModels.add(consumeQueueMMapFileModel);
        }
        CommonCache.getConsumerQueueMMapFileModelManager().put(topicName, consumeQueueMMapFileModels);
    }
}
